package java_thinking.unit_18_IO._18_6;

import java.io.*;

/**
 * @Desc:   基本文件的输出
 *          1.我们通常会用BufferedWriter将输出的内容包装以缓冲输出，效率高，
 *          2.为了提供格式化机制，BufferedWriter 被装饰成 PrintWriter
 * @author: cww
 * @DateTime: 2019/5/14 19:17
 */

public class BasicFileOutput {
    static String file = "src/maindemo/java/java_thinking/unit_18_IO/_18_6/BasicFileOutput.txt";
    static String file1 = "src/maindemo/java/java_thinking/unit_18_IO/_18_6/BasicFileOutput1.txt";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(
                new StringReader(BufferedInputFile.read("src/maindemo/java/java_thinking/unit_18_IO/_18_6/BasicFileOutput.java")));
        //格式化
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        int linCount = 1;
        String s;
        while ((s = in.readLine()) != null) {
            out.println(linCount++ + ": " + s);
        }
//        out.flush();
        out.close();
        System.out.println(BufferedInputFile.read(file));

        BufferedReader in1 = new BufferedReader(
                new StringReader(BufferedInputFile.read("src/maindemo/java/java_thinking/unit_18_IO/_18_6/BasicFileOutput.java")));
        int linCount1 = 1;
        String s1;
        //未格式化
        BufferedWriter writer = new BufferedWriter(new FileWriter(file1));
        while ((s1 = in1.readLine()) != null) {
//            writer.write(linCount1++ + ": " + s1 + "\n");
            writer.write(linCount1++ + ": " + s1);
        }
        writer.close();
        System.out.println(BufferedInputFile.read(file1));

    }
}
